package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsServiceI;
import com.dongdongshop.util.OSSUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("tbGoodsController")
public class TbGoodsController {

    @Reference
    private TbGoodsServiceI tbGoodsServiceI;

    private Logger logger= LoggerFactory.getLogger(TbSellerController.class);

    //跳转添加商品页面
    @RequestMapping("getFindAll")
    public String getFindAll(){
        return "admin/goods_edit";
    }

    //三级联动，商品类型
    @RequestMapping("getTbItemCat")
    @ResponseBody
    public List<TbItemCat> getTbItemCat(Long pid){
        List<TbItemCat> tbItemCat = tbGoodsServiceI.getTbItemCat(pid);
        return tbItemCat;
    }

    //根据商品类型pid 当做id 查询
    @RequestMapping("getTbTypeTemplate")
    @ResponseBody
    public TbTypeTemplate getTbTypeTemplate(Long id){
        try {
            TbTypeTemplate tbTypeTemplate =  tbGoodsServiceI.selectTbTypeTemplateById(id);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",tbTypeTemplate);
            return tbTypeTemplate;
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
            return null;
        }

    }

    //确认增加
    @RequestMapping("saveTbGoods")
    @ResponseBody
    public SpecificPrint saveTbGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String items,TbItem tbItem){
        //1.获取subiect
        Subject subject = SecurityUtils.getSubject();
        TbSeller tbSeller = (TbSeller)subject.getPrincipal();
        tbGoods.setSellerId( tbSeller.getSellerId());
        tbGoodsServiceI.savetbGoods(tbGoods,tbGoodsDesc,items,tbItem);
        try {
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbGoods);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbGoods);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //上传图片
    @RequestMapping(value = "uploadImage",method = RequestMethod.POST)
    @ResponseBody
    public SpecificPrint uploadImage(@RequestParam("myFile") MultipartFile myFile){
        try {
            //返回上传后的url地址
            String url = OSSUtil.upload(myFile);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(url);
        } catch (IOException e) {
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }


    // 根据specId，查询规格回显
    @RequestMapping("getFildAll")
    @ResponseBody
    public SpecificPrint getFildAll(@RequestParam("specIds[]")Integer[] specIds){
        try {
            //通过规格id去查询规格分类中的id 条件查询获取所有的对应字段
            Map<Integer,List<TbSpecificationOption>> map = tbGoodsServiceI.getFildAll(specIds);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(map);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //跳转商品管理
    @RequestMapping("getFindAlls")
    public String getFindAlls(){
        return "admin/goods";
    }

    //跳转商品管理
    @RequestMapping("getGoods")
    @ResponseBody
    public SpecificPrint getGoods(String auditStatus,String goodsName){
        //1.获取subiect
        Subject subject = SecurityUtils.getSubject();
        TbSeller tbSeller = (TbSeller)subject.getPrincipal();
        String id = tbSeller.getSellerId();
        try {
            List<TbGoodsVo> tbGoods = tbGoodsServiceI.getFindAlls(id,auditStatus,goodsName);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbGoods);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    //删除
    @RequestMapping("deleteGoods")
    @ResponseBody
    public SpecificPrint deleteGoods(String ids){
        try {
            //截取字符串
            String idss=ids.replaceAll("'","");
            String[] split = idss.split(",");
            //遍历出所有选中的ID
            List<Long> list=new ArrayList<>();
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
            tbGoodsServiceI.UpdateGoods(list);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //提交
    @RequestMapping("sumitGoods")
    @ResponseBody
    public SpecificPrint sumitGoods(String ids){
        try {
            //截取字符串
            String idss=ids.replaceAll("'","");
            String[] split = idss.split(",");
            //遍历出所有选中的ID
            List<Long> list=new ArrayList<>();
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
            tbGoodsServiceI.sumitGoods(list);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

}
